{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python375jvsc74a57bd0971de8236d001e156202e75ab73a66f12817a181b09f6a4f3f5c915b9ed55cc8",
   "display_name": "Python 3.7.5 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[Warning]\"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\test\" already exists! (You can ignore this warning)\n[Warning]\"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\" already exists! (You can ignore this warning)\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import re\n",
    "import os \n",
    "import sys\n",
    "ROOT_PATH = os.getcwd()\n",
    "TEST_DIR = os.path.join(ROOT_PATH, 'test')\n",
    "AUDIO_DIR = os.path.join(ROOT_PATH, 'audio files')\n",
    "for dir in [TEST_DIR, AUDIO_DIR]:\n",
    "    try:\n",
    "        os.mkdir(dir)\n",
    "    except:\n",
    "        print('[Warning]\"{}\" already exists! (You can ignore this warning)'.format(dir))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'http://download.dogwood.com.cn/online/6jchlx/01.mp3'"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "base_url = 'http://download.dogwood.com.cn/online/6jchlx/'\n",
    "amount = 32\n",
    "audio_url_list = [ base_url+'%02d'%i+'.mp3'  for i in range(1,33)]\n",
    "audio_url_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "response = requests.get(audio_url_list[0])\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "b'ID3\\x03\\x00\\x00\\x00\\x00\\x01r'"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "audio_binStream = response.content # 获取二进制流数据（字节流）\n",
    "# audio_binStream # 太多了展示不全\n",
    "audio_binStream[:10] # 这里就展示前10个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "bytes"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "type(audio_binStream) # 可以看看类型，是bytes类型（字节流）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "with open('./test/01.mp3', 'wb') as fp:\n",
    "    fp.write(audio_binStream)\n",
    "    print('Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'http://download.dogwood.com.cn/online/6jchlx/01.mp3'"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "audio_url_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'3pm.10/xlhcj6/enilno/nc.moc.doowgod.daolnwod//:ptth'"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "test = audio_url_list[0][::-1]\n",
    "test\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(6, 7)"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "re.search('/', test).span()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'01.mp3'"
      ]
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "audio_url_list[0][-re.search('/', test).span()[0]:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def file_downloader_old(url:str, save_dir_path:str, file_name:str=None):\n",
    "    ''' 文件下载器，一次读取文件的所有字节流，然后写入成文件 \n",
    "        如果未指定名称，则提取url末尾的文件原名命名\n",
    "    '''\n",
    "    if file_name == None:\n",
    "        url_reversed = url[::-1]\n",
    "        file_name = url[-re.search('/', url_reversed).span()[0]:]\n",
    "    try:\n",
    "        os.mkdir(save_dir_path)\n",
    "        print('[Warning]The save path \"{}\" isn\\'t exist, now created !(You can ignore this warning)'.format(save_dir_path))\n",
    "    except:\n",
    "        pass\n",
    "    file_save_path = os.path.join(save_dir_path, file_name)\n",
    "    response = requests.get(url)\n",
    "    file_byteStream = response.content\n",
    "    try:\n",
    "        with open(file_save_path, 'wb') as fp:\n",
    "            fp.write(file_byteStream)\n",
    "            print('Your file has been successfully downloaded at \"{}\"'.format(file_save_path))\n",
    "    except:\n",
    "        print('[Err]Oops, some errors have occurred, please try later!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\test\\02.mp3\"\n"
     ]
    }
   ],
   "source": [
    "file_downloader_old(audio_url_list[1], TEST_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "response = requests.get(audio_url_list[2])\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<generator object iter_slices at 0x0000028E8DEA9548>"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "byte_chunk_list = response.iter_content(chunk_size=128)\n",
    "byte_chunk_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# byte_chunk_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "b'ID3\\x03\\x00\\x00\\x00\\x00\\x01rTIT2\\x00\\x00\\x00\\r\\x00\\x00\\x00word list 03TRCK\\x00\\x00\\x00\\x02\\x00\\x00\\x003COMM\\x00\\x00\\x00%\\x00\\x00\\x00chi\\x00\\xd0\\xc2\\xb6\\xab\\xb7\\xbd\\xcd\\xbc\\xca\\xe9\\xcd\\xf8\\xa3\\xbawww.dogwood.com.cnTYER\\x00\\x00\\x00\\x05\\x00\\x00\\x002014TCON\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\xd0\\xc2\\xb6\\xab\\xb7\\xbdTALB'"
      ]
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "byte_chunk_list = list(byte_chunk_list)\n",
    "byte_chunk_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "128"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "len(byte_chunk_list[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Writing data...\nDone!\n"
     ]
    }
   ],
   "source": [
    "# 编写一个Python程序，用于清空Python控制台 \n",
    "# https://zhuanlan.zhihu.com/p/62629768\n",
    "with open('./test/03.mp3', 'wb') as fp:\n",
    "    print('Writing data...')\n",
    "    for chunk in byte_chunk_list:\n",
    "        fp.write(chunk)\n",
    "print('Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://blog.csdn.net/mrjiale/article/details/81436380\n",
    "def file_downloader_in_chunks(url:str, save_dir_path:str, file_name=None, chunk_size:int=128):\n",
    "    if file_name == None:\n",
    "        url_reversed = url[::-1]\n",
    "        file_name = url[-re.search('/', url_reversed).span()[0]:]\n",
    "    try:\n",
    "        os.mkdir(save_dir_path)\n",
    "        print('[Warning]The save path \"{}\" isn\\'t exist, now created !(You can ignore this warning)'.format(save_dir_path))\n",
    "    except:\n",
    "        pass\n",
    "    file_save_path = os.path.join(save_dir_path, file_name)\n",
    "    response = requests.get(url, stream=True)\n",
    "    try:\n",
    "        with open(file_save_path, 'wb') as fp:\n",
    "            print('Writing data...')\n",
    "            for chunk in response.iter_content(chunk_size=chunk_size):\n",
    "                fp.write(chunk)\n",
    "            print('Your file has been successfully downloaded at \"{}\"'.format(file_save_path))\n",
    "    except:\n",
    "        print('[Err]Oops, some errors have occurred, please try later!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Writing data...\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\test\\04.mp3\"\n"
     ]
    }
   ],
   "source": [
    "file_downloader_in_chunks(audio_url_list[3], TEST_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\01.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\02.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\03.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\04.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\05.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\06.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\07.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\08.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\09.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\10.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\11.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\12.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\13.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\14.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\15.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\16.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\17.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\18.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\19.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\20.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\21.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\22.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\23.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\24.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\25.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\26.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\27.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\28.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\29.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\30.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\31.mp3\"\n",
      "Your file has been successfully download at \"c:\\Users\\rg4sun\\Desktop\\GetVacabularyMP3\\audio files\\32.mp3\"\n",
      "Done!\n"
     ]
    }
   ],
   "source": [
    "for url in audio_url_list:\n",
    "    file_downloader_old(url, AUDIO_DIR)\n",
    "print('Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[Warning]The save path \"./pdf-test\" isn't exist, now created !(You can ignore this warning)\n",
      "Your file has been successfully download at \"./pdf-test\\Edge-Native-Technical-Architecture-White-Paper.pdf\"\n"
     ]
    }
   ],
   "source": [
    "test_pdf_url = 'https://pmo32e887-pic2.ysjianzhan.cn/upload/Edge-Native-Technical-Architecture-White-Paper.pdf'\n",
    "file_downloader_old(test_pdf_url, './pdf-test')"
   ]
  }
 ]
}